package org.jeecg.modules.yw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.yw.entity.Category;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.service.CategoryService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.jeecg.modules.yw.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(tags = "分类管理")
@RequestMapping("/app/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @ApiOperation(value = "获取分类列表")
    @GetMapping("/page")
    public  org.jeecg.common.api.vo.Result page(Page<Category> page,
                                                @RequestParam(value = "name", required = false) String name,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize ) {
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (name != null && !"".equals(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("create_time");
        IPage<Category> result = categoryService.page(page, queryWrapper);
        return  org.jeecg.common.api.vo.Result.ok(result);
    }
    @ApiOperation(value = "获取分类列表")
    @GetMapping("/List")
    public Result list(Page<Category> page) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("name");
        List<Category> result = categoryService.list(queryWrapper);
        return Result.success(result);
    }
    @ApiOperation(value = "添加分类")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody CategoryVo categoryVo, HttpServletRequest request) throws Exception {
        Category category = new Category();
        BeanUtils.copyProperties(categoryVo, category);
        category.setId(CommonUtils.getID());
        category.setCreateTime(DateUtils.getCurrentDate());
        category.setCreator(UserUtil.getUserId(request, sysBaseAPI));
        categoryService.save(category);
        return Result.success();
    }
    @ApiOperation(value = "修改分类")
    @PostMapping("/editCategory")
    public Result editCategory(@RequestBody CategoryVo categoryVo) throws Exception {
        Category category = new Category();
        BeanUtils.copyProperties(categoryVo, category);
        categoryService.updateById(category);
        return Result.success();
    }
    @ApiOperation(value = "删除分类")
    @DeleteMapping("/deleteCategory")
    public org.jeecg.common.api.vo.Result deleteCategory(@RequestParam(value = "id") String id) {
        categoryService.removeById(id);
        return org.jeecg.common.api.vo.Result.ok("删除成功");
    }
}
